package cn.wbnull.springbootdemo.io;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;

/**
 * 功能描述
 *
 * @author EDZ
 * @since 2022-09-01
 */
class ioTest {
    public static void main(String[] args) throws IOException {
         File file = new File("C:\\Windows");
         File file1 = new File("E:\\sb");
        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getCanonicalPath());
        System.out.println(file.separator);
        System.out.println(file.isFile());
        System.out.println(file1.isFile());
        System.out.println(file.isDirectory());
        System.out.println(file1.isDirectory());
        if(file1.isFile()){
            file1.delete();
            System.out.println("删除成功");
        }
        else {
            file1.createNewFile();
            System.out.println("添加成功");
            file1.delete();
//            System.out.println("添加之后删除");
        }

        //临时文件
         File file2 = File.createTempFile("tmp-",".txt");
        System.out.println(file2.getAbsolutePath());
        //Jvm退出是自动删除
        file2.deleteOnExit();
        System.out.println(file2.isDirectory());
        System.out.println(file2.isFile());
        System.out.println(file2.getAbsolutePath());


         File file3 = new File("C:\\Windows");
         //列出所有文件和子目录
         File[] files = file3.listFiles();
        System.out.println(files);
        if (files != null){
            for (File f : files){
                System.out.println(f);
            }
        }
        System.out.println("======================================");
         File[] files1 = file3.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".exe");
            }
        });
        if (files1 != null){
            for (File f : files1){
                System.out.println(f);
            }
        }
        System.out.println("=================================================");
        //构建Path对象
         Path path = Paths.get(".", "project", "study");
        System.out.println(path);
        //绝对路径
        System.out.println(path.toAbsolutePath());
        //规范模式
        System.out.println(path.normalize());
         Path normalize = path.normalize();
        //转换成File对象
         File toFile = normalize.toFile();
        System.out.println(toFile);
        //遍历绝对路径
        for (Path p : Paths.get("..").toAbsolutePath()){
            System.out.println(""  + p);
        }

        System.out.println("=========================over===============");




    }
}
